//
//  MainTainNotificaionForIPadVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "MainTainNotificationForIPadVC.h"
#import "SearchForDiseaseListIPadCell.h"
#import "PutNormolForIPadCell.h"
#import "SearchForDiseaseDetailForIPadCell.h"
#define ListCellID @"cellid1"
#define PutCellID  @"cellid2"
#define DetailCellID @"cellid3"
@interface MainTainNotificationForIPadVC ()<DetailForIPadCellDelegate>
{
    NSInteger _selectedIndex;//选中查看第几个
    BOOL    _isAddState;//1，添加 0，查看
}
@property (nonatomic) NSInteger state;//0未上报  1已上报
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) NSString *codeId;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) UIView *rightFooterView;
@property (nonatomic,strong) NSString *startName;//起始桩号
@property (nonatomic,strong) NSString *endName;//终止桩号


@end

@implementation MainTainNotificationForIPadVC


#pragma mark - Life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self resetNav];
    [self setTableViewFooterView];
      [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self setWebData];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"病害巡查编码",@"路线编码",@"行车方向",@"起点桩号",@"终点桩号",@"病害具体情况",@"通知单编码",@"发起部门",@"接收部门",@"维修部门",@"质量要求",@"维修开始日期",@"维修结束日期",@"签发人",@"签发日期",@"日常养护措施",@"备注"];
    }
    return _titleArray;
}

#pragma mark - UI
- (void)resetNav{
    self.leftTitle = @"首页";
    self.pullRefreshEnable = YES;
    self.pullLoadMoreEnable = YES;
    self.myTitle.text = @"维修通知";
  
    [self.tableView registerClass:[SearchForDiseaseListIPadCell class] forCellReuseIdentifier:ListCellID];
    [self.rightTableView registerClass:[PutNormolForIPadCell class] forCellReuseIdentifier:PutCellID];
    [self.rightTableView registerClass:[SearchForDiseaseDetailForIPadCell class] forCellReuseIdentifier:DetailCellID];
}
- (void)setTableViewFooterView{
    
    
    self.rightFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.rightTableView.frame.size.width, 212*PadHeightRate)];
    self.rightFooterView.backgroundColor = Bai_Color;
    self.rightTableView.tableFooterView = self.rightFooterView;
    
    
    UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
    submit.layer.cornerRadius = 5;
    submit.layer.masksToBounds = YES;
    submit.backgroundColor = MAIN_Color;
    [submit setTitleColor:Bai_Color forState:UIControlStateNormal];
    [submit setTitle:@"生成维修单" forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    submit.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:submit];
    [submit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-135*PadHeightRate);
        make.height.mas_equalTo(45*PadHeightRate);
        make.right.mas_equalTo(-30*PadWidthRate);
        make.left.mas_equalTo(30*PadWidthRate);
    }];
}
#pragma mark - UI Response


- (void)btnClick{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确认" message:@"确定生成维修单吗?" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [NetWorkTool becomeMaintentanceWithMaintentanceNoticeId:self.dic[@"maintentanceNoticeId"] block:^(id data) {
           [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"生成维修单成功" animated:YES];
                    
                    
                    self.page = 1;
                    [self setWebData];
                    
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
    
        
    
        
    }

- (void)normolState:(NSInteger)state{
    if (state) {
        self.rightTableView.tableFooterView.hidden = YES;
    }else{
        self.rightTableView.tableFooterView.hidden = NO;
    }
    self.myCodeTitle.text = self.dataArray[_selectedIndex][@"billNo"];
}
#pragma mark - Data handle
- (void)setWebData{
  
    [NetWorkTool getMainTainNotificationWithPage:self.page pageSize:10 block:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        [self performSelector:@selector(MJRefreshStop) withObject:nil afterDelay:1];
        if (data) {
            if ([data[@"success"] integerValue]) {
                if (self.page == 1) {
                    self.tableView.contentOffset = CGPointMake(0, 0);
                    _selectedIndex = 0;
                    [self.dataArray removeAllObjects];
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }else{
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }
                if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                    if ([data[@"data"][@"items"] count]<10&&(self.dataArray.count!=0)) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                if (self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                    self.rightTableView.tableFooterView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.codeId = self.dataArray[0][@"maintentanceNoticeId"];
                    NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[0][@"state"]];
                    if ([str integerValue]==1||[str integerValue]==3) {
                        self.state = 1;
                    }else{
                        self.state = 0;
                    }
                    [self setWebDataDetail];
                    [self normolState:self.state];
                }
                [self.tableView reloadData];
                self.rightTableView.contentOffset = CGPointMake(0, 0);
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
            
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES
             ];
        }
    }];
}
- (void)setWebDataDetail{
    self.rightTableView.contentOffset = CGPointMake(0, 0);
    [self.rightTableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
    [NetWorkTool getMainTainNotificationDetailWithID:self.codeId block:^(id data) {
    [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"];
                self.startName = [NSString stringWithFormat:@"%@",self.dic[@"startStake"]] ;
                self.endName = [NSString stringWithFormat:@"%@",self.dic[@"endStake"]];
                self.myCodeTitle.text = self.dataArray[_selectedIndex][@"billNo"];
                [self.rightTableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}
#pragma mark - Delegate
- (void)detailForIPadCellWithString:(NSString *)str indexPath:(NSIndexPath *)indexPath{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.tableView]) {
        return self.dataArray.count;
        
    }else{
        return 17;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = (SearchForDiseaseListIPadCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHeight;
    }else{
        if (indexPath.row==0) {
            return 68*PadHeightRate;
        }else if(indexPath.row == 15||indexPath.row==16){
            return 152*PadHeightRate;
        }
        else{
            return 50*PadHeightRate;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
        if (_selectedIndex == indexPath.row) {
            cell.contentView.backgroundColor = Bai_Color;
            cell.contentView.layer.borderWidth = 0;
            cell.blueLine.hidden = NO;
        }else{
            cell.contentView.backgroundColor = [PublicMethod colorWithHexString:@"#f4f4f4" andAlpha:1];
            cell.contentView.layer.borderColor = [Line_Color CGColor];
            cell.contentView.layer.borderWidth = 1;
            cell.blueLine.hidden = YES;
        }
        [cell mainTainNotificationAddValue:self.dataArray[indexPath.row]];
        return cell;
    }else{
        if(indexPath.row ==15||indexPath.row==16){
            SearchForDiseaseDetailForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
            cell.titleLab.text = self.titleArray[indexPath.row];
            cell.delegate = self;
            cell.indexPath = indexPath;
            if (indexPath.row ==15) {
                cell.titleLab.text = @"日常养护措施";
                cell.detailTextView.text = self.dic[@"measureName"];
            }else{
                cell.titleLab.text = @"备注";
                cell.detailTextView.text = self.dic[@"remark"];
            }
            cell.detailTextView.placeholder = @" ";
            cell.detailTextView.editable = NO;
            return cell;
        }else{
            PutNormolForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
            cell.titleLab.text = self.titleArray[indexPath.row];
            switch (indexPath.row) {
                case 0:
                {
                    cell.contentLab.text = self.dic[@"diseaseInspectionNo"];
                }
                    break;
                case 1:
                {
                    cell.contentLab.text = self.dic[@"routeCode"];
                }
                    break;
                case 2:
                {
                    cell.contentLab.text = self.dic[@"drivingDirectionName"];
                }
                    break;
                case 3:
                {
                    NSString *num = [NSString stringWithFormat:@"%.3f",[self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000];
                    if ([num integerValue] == [num floatValue]) {
                        num = [NSString stringWithFormat:@"%ld",[num integerValue]];
                    }
                    if (([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)<10) {
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+00%@",[self.startName integerValue]/1000,num];
                    }else if(([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)>=10&&([self.startName doubleValue]-floor([self.startName doubleValue]/1000)*1000)<100){
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+0%@",[self.startName integerValue]/1000,num];
                    }else{
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+%@",[self.startName integerValue]/1000,num];
                    }
                }
                    break;
                case 4:
                {
                    NSString *num = [NSString stringWithFormat:@"%.3f",[self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000];
                    if ([num integerValue] == [num floatValue]) {
                        num = [NSString stringWithFormat:@"%ld",[num integerValue]];
                    }
                    if (([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)<10) {
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+00%@",[self.endName integerValue]/1000,num];
                    }else if(([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)>=10&&([self.endName doubleValue]-floor([self.endName doubleValue]/1000)*1000)<100){
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+0%@",[self.endName integerValue]/1000,num];
                    }else{
                        cell.contentLab.text = [NSString stringWithFormat:@"K%ld+%@",[self.endName integerValue]/1000,num];
                    }
                    
                }
                    break;
                case 5:
                {
                    if(self.dic){
                        if ([self.dic[@"diseaseLevelName"] length]==0) {
                            cell.contentLab.text = [NSString stringWithFormat:@"%@%@%@%@",self.dic[@"diseaseLevelName"],self.dic[@"diseaseFormName"],[PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.dic[@"num"] doubleValue]]],self.dic[@"measureUnitName"]];
                        }else{
                            cell.contentLab.text = [NSString stringWithFormat:@"%@度%@%@%@",self.dic[@"diseaseLevelName"],self.dic[@"diseaseFormName"],[PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.dic[@"num"] doubleValue]]],self.dic[@"measureUnitName"]];
                        }
                    }
                }
                    break;
                case 6:
                {
                    cell.contentLab.text = self.dic[@"billNo"];
                }
                    break;
                case 7:
                {
                    cell.contentLab.text = self.dic[@"lanuchDepartmentName"];
                }
                    break;
                case 8:
                {
                    cell.contentLab.text = self.dic[@"receiveDepartmentName"];
                }
                    break;
                case 9:
                {
                    cell.contentLab.text = self.dic[@"maintenanceDepartmentName"];
                }
                    break;
                case 10:
                {
                    cell.contentLab.text = self.dic[@"qualityRequirement"];
                }
                    break;
                case 11:
                {
                    cell.contentLab.text = self.dic[@"startDate"];
                }
                    break;
                case 12:
                {
                    cell.contentLab.text = self.dic[@"endDate"];
                }
                    break;
                case 13:
                {
                    cell.contentLab.text = self.dic[@"userName"];
                }
                    break;
                case 14:
                {
                    cell.contentLab.text = self.dic[@"signDate"];
                }
                    break;
                default:
                    break;
            }
            cell.img.hidden = YES;
            cell.img.image = [UIImage imageNamed:@"enter"];
            
            return cell;
        }
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        _selectedIndex = indexPath.row;
        [self.tableView reloadData];
        self.codeId = self.dataArray[indexPath.row][@"maintentanceNoticeId"];
        NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"state"]];
        if ([str integerValue]==1||[str integerValue]==3) {
            self.state = 1;
        }else{
            self.state = 0;
        }
        [self setWebDataDetail];
        [self normolState:self.state];
    }else{
        
    }
    
}
@end
